package com.lzj.controller;

import com.lzj.dao.DepartmentMapper;
import com.lzj.entity.Department;
import com.lzj.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *测试是否能够连接数据库
 * </p>
 *
 * @autor:lzj
 * @date:2022/2/26
 */
@Controller
@RequestMapping("/admin")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @RequestMapping("/departments")
    public String departments(Model model){
        model.addAttribute("deps",departmentService.getAllDeps());
        return "departments";
    }

    @PostMapping("/addDepartment")
    public String addDepartment(String departmentname,Model model){
        Department depByName = departmentService.getDepByName(departmentname);
        if(depByName!=null){
            System.out.println("进入了这个判断");
            model.addAttribute("error","已存在");
        }else{
            int i = departmentService.addDepartment(departmentname);
        }

        return "forward:/admin/departments";
    }

    @RequestMapping("/deleteDep/{departmentid}")
    public String deleteDep(@PathVariable int departmentid){
        int i = departmentService.deleteDep(departmentid);
        return "redirect:/admin/departments";
    }

    @RequestMapping("/updateDep")
    @ResponseBody
    public String updateDep(int id,String name){
        int i = departmentService.updateDep(id, name);
        if(i==1){
            return "success";
        }
        return "error";
    }


}
